import axios from "axios";
import Element from "element-ui"
import access from './config/access'
import desHeader from "./util/desHeader";

axios.defaults.baseURL = access.serverUrl.localhost

const request = axios.create({
    timeout: 65000,
    headers: {
        'Content-Type': "application/json; charset=utf-8",
        'If-Modified-Since':'0',
        'Cache-Control':'no-cache'
    }
})

request.interceptors.request.use(config => {
    config.headers['token'] = localStorage.getItem("token")
    config.headers['Accept-page-code'] = desHeader.getHeader2()
    config.headers['ip'] = localStorage.getItem("ip")
    config.headers['vefCode'] = localStorage.getItem("vefCode")===null?"0000":localStorage.getItem("vefCode")
    return config
})

request.interceptors.response.use(
    response => {
        if (response.data.code === 200){
            return response
        }else if(response.data.code === -2 || response.data.code === -5) {
            localStorage.removeItem('token')
            Element.Message.error(response.data.msg, {duration: 1500})
            setTimeout(()=>{
                window.location.href = "/";
            },2000)
        }else {
            Element.Message.error(response.data.msg, {duration: 3000})
            return Promise.reject(response.data.msg)
        }
    },
    error => {
        Element.Message.error("服务器出小差，请稍后再试", {duration: 3000})
        return Promise.reject(error)
    }
)

export default request
